namespace Sage.Http.Core.Handlers
{
    /// <summary>
    /// 请求配置处理器，负责处理所有与请求配置相关的逻辑
    /// </summary>
    /// <remarks>
    /// 初始化请求配置处理器
    /// </remarks>
    /// <param name="request">HTTP请求消息</param>
    internal class RequestConfigHandler(HttpRequestMessage request)
    {
        private readonly HttpRequestMessage _request = request ?? throw new ArgumentNullException(nameof(request));
        private IProgress<float>? _progress;
        private CancellationToken _cancellationToken = default;

        /// <summary>
        /// 获取进度报告接口
        /// </summary>
        public IProgress<float>? Progress => _progress;

        /// <summary>
        /// 获取取消令牌
        /// </summary>
        public CancellationToken CancellationToken => _cancellationToken;

        /// <summary>
        /// 禁用此请求的日志记录
        /// </summary>
        public void DisableLogging()
        {
            _request.Options.Set(new HttpRequestOptionsKey<bool>("DisableLogging"), true);
        }

        /// <summary>
        /// 配置请求超时时间
        /// </summary>
        /// <param name="timeout">超时时间</param>
        public void SetTimeout(TimeSpan timeout)
        {
            HttpRequestManager.SetRequestTimeout(_request, timeout);
        }

        /// <summary>
        /// 配置上传进度报告接口
        /// </summary>
        /// <param name="progress">进度报告接口</param>
        public void SetProgress(IProgress<float> progress)
        {
            _progress = progress;
        }

        /// <summary>
        /// 配置取消令牌
        /// </summary>
        /// <param name="cancellationToken">取消令牌</param>
        public void SetCancellationToken(CancellationToken cancellationToken)
        {
            _cancellationToken = cancellationToken;
        }

        /// <summary>
        /// 清除所有配置
        /// </summary>
        public void Clear()
        {
            _progress = null;
            _cancellationToken = default;
            // 注意：不清除已设置的请求选项，因为它们已经应用到请求中
        }
    }
}